package cn.iocoder.yudao.module.digital.service.worksaudio;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.digital.controller.admin.worksaudio.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.worksaudio.WorksAudioDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.digital.dal.mysql.worksaudio.WorksAudioMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.digital.enums.ErrorCodeConstants.*;

/**
 * 数字人声音作品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WorksAudioServiceImpl implements WorksAudioService {

    @Resource
    private WorksAudioMapper worksAudioMapper;

    @Override
    public Long createWorksAudio(WorksAudioSaveReqVO createReqVO) {
        // 插入
        WorksAudioDO worksAudio = BeanUtils.toBean(createReqVO, WorksAudioDO.class);
        worksAudioMapper.insert(worksAudio);
        // 返回
        return worksAudio.getId();
    }

    @Override
    public void updateWorksAudio(WorksAudioSaveReqVO updateReqVO) {
        // 校验存在
        validateWorksAudioExists(updateReqVO.getId());
        // 更新
        WorksAudioDO updateObj = BeanUtils.toBean(updateReqVO, WorksAudioDO.class);
        worksAudioMapper.updateById(updateObj);
    }

    @Override
    public void deleteWorksAudio(Long id) {
        // 校验存在
        validateWorksAudioExists(id);
        // 删除
        worksAudioMapper.deleteById(id);
    }

    private void validateWorksAudioExists(Long id) {
        if (worksAudioMapper.selectById(id) == null) {
            throw exception(WORKS_AUDIO_NOT_EXISTS);
        }
    }

    @Override
    public WorksAudioDO getWorksAudio(Long id) {
        return worksAudioMapper.selectById(id);
    }

    @Override
    public PageResult<WorksAudioDO> getWorksAudioPage(WorksAudioPageReqVO pageReqVO) {
        return worksAudioMapper.selectPage(pageReqVO);
    }

}